package dao

import (
	"fmt"
	"hft/Database/admin"
	"hft/Http/Models/request"

	"gorm.io/gorm"
)

type GetAccount struct{}

func (g *GetAccount) JustRegion(regionId int, db *gorm.DB, SearchAccount *admin.Account) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		var totalReg = fmt.Sprintf("%v%v,%%", SearchAccount.AccountLink, SearchAccount.ID)
		return db.Where("account_link LIKE ?", totalReg)
	}
}

func (g *GetAccount) Preload(db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Preload("Regions", func(db *gorm.DB) *gorm.DB {
			return db.Omit("link")
		}).Preload("Role")
	}
}

func (g *GetAccount) Omit(db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Omit("password,link,belong")
	}
}

func (g *GetAccount) Phone(db *gorm.DB, is *request.AccountRegion) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if is.Phone == "" {
			return db
		} else {
			return db.Where("phone = ?", is.Phone)
		}

	}
}

func (g *GetAccount) IDCard(db *gorm.DB, is *request.AccountRegion) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if is.IDCard == "" {
			return db
		} else {
			return db.Where("id_card = ?", is.IDCard)
		}
	}
}

func (g *GetAccount) Is(db *gorm.DB, is *request.AccountRegion) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Scopes(g.Phone(db, is)).Scopes(g.IDCard(db, is))
	}
}

func (g *GetAccount) Username(db *gorm.DB, like *request.AccountSearchInfo) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if like.Username == "" {
			return db
		} else {
			return db.Where("username LIKE ?", fmt.Sprintf("%%%v%%", like.Username))
		}
	}
}

func (g *GetAccount) Nickname(db *gorm.DB, like *request.AccountSearchInfo) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if like.RealName == "" {
			return db
		} else {
			return db.Where("real_name LIKE ?", fmt.Sprintf("%%%v%%", like.RealName))
		}
	}
}

func (g *GetAccount) Like(db *gorm.DB, like *request.AccountSearchInfo) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Scopes(g.Username(db, like)).Scopes(g.Nickname(db, like))
	}
}

func (g *GetAccount) Status(db *gorm.DB, status bool) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Where("status = ?", status)
	}
}

func (g *GetAccount) GetAccountList(SearchAccount *admin.Account, paginate *request.AccountPage, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Where("link LIKE ?", fmt.Sprintf("%v%%", SearchAccount.Link)).Scopes(g.Status(db, paginate.Status)).Scopes(BaseDao.CreateTime(db, paginate.CreateTime)).Scopes(g.Role(paginate.RoleId, db)).Scopes(g.Like(db, &paginate.AccountSearchInfo)).Scopes(g.Is(db, &paginate.AccountRegion)).Scopes(g.JustRegion(int(paginate.RegionId), db, SearchAccount)).Scopes(Paginate(Page(paginate.Page), db)).Scopes(g.Omit(db)).Scopes(g.Preload(db)).Order("sort desc")
	}
}

func (g *GetAccount) Role(roleId int, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if roleId == 0 {
			return db
		} else {
			return db.Where("role_id = ?", roleId)
		}
	}
}

func (g *GetAccount) Filter(db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Joins("AccountExpand").Not("can_bind_lou_pan = ?", 1)
	}
}

var GetAccountDao = new(GetAccount)
